﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace CXMLStudioIDE.DataAccessLayer
{
    public class UserManagementDAL : BaseDAL
    {
        public int GetCXMLUserIdByName(string sZ_NetworkID)
        {
            int i_UserID = -1;
            DataSet DS = new DataSet();
            System.Data.SqlClient.SqlConnection con = GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("Select idgk_CXMLUserID from dbo.CXMLUser where vch_NetworkUserID = '" + sZ_NetworkID + "'", con);
            con.Open();
            da.Fill(DS);
            da.Dispose();
            con.Close();
            if (DS.Tables.Count > 0 && DS.Tables[0].Rows.Count > 0)
                i_UserID = (int)DS.Tables[0].Rows[0].ItemArray[0];
            return i_UserID;
        }

        public string GetCXMLUserNameById(int i_NetworkID)
        {
            string sZ_CXMLUser = "";
            DataSet DS = new DataSet();
            System.Data.SqlClient.SqlConnection con = GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("Select vch_NetworkUserID from dbo.CXMLUser where idgk_CXMLUserID = " + i_NetworkID, con);
            con.Open();
            da.Fill(DS);
            da.Dispose();
            con.Close();
            if (DS.Tables.Count > 0 && DS.Tables[0].Rows.Count > 0)
                sZ_CXMLUser = DS.Tables[0].Rows[0].ItemArray[0].ToString();
            return sZ_CXMLUser;
        }

        public string GetRoleFromUser(string sZ_NetworkID)
        {
            DataSet DS = new DataSet();
            System.Data.SqlClient.SqlConnection con = GetConnection();
            System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("dbo.GetRoleFromUser", con);
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
            da.SelectCommand.Parameters.AddWithValue("CXMLUserName", sZ_NetworkID);
            con.Open();
            da.Fill(DS);
            da.Dispose();
            con.Close();
            if (DS.Tables[0].Rows.Count > 0)
            {
                return DS.Tables[0].Rows[0]["CXMLUserName"].ToString();
            }
            return "";
        }
    }
}
